<?php


namespace api\modules\v1\controllers\buss;

use common\models\buss\Devices;
use services\buss\ScardService;
use services\member\MemberService;
use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\DevicesService;

class DevicesController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/devices/index 设备列表
     * @apiGroup Devices
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} keywords               搜索词
     * @apiParam {int} project_id                项目ID
     * @apiParam {int} use_mid                   使用人
     * @apiParam {int} status                    状态
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {Number}  data.list.project_id       项目ID
     * @apiSuccess {String}  data.list.phone_model      手机型号
     * @apiSuccess {String}  data.list.name             名称
     * @apiSuccess {String}  data.list.sim_card         手机号
     * @apiSuccess {Array}   data.list.sim_card_id      手机号ID数组
     * @apiSuccess {Number}  data.list.use_mid          使用人ID
     * @apiSuccess {String}  data.list.use_mname        使用人
     * @apiSuccess {String}  data.list.operation_name   操作人
     * @apiSuccess {Number}  data.list.operation_id     操作人ID
     * @apiSuccess {String}  data.list.remark           备注
     * @apiSuccess {Number}  data.list.sort             排序
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.project_arr                 项目筛选
     * @apiSuccess {Number}  data.condition.project_arr.value           项目ID
     * @apiSuccess {String}  data.condition.project_arr.label           项目名称
     * @apiSuccess {String}  data.condition.status_arr                  状态筛选
     * @apiSuccess {Number}  data.condition.status_arr.value            状态ID
     * @apiSuccess {String}  data.condition.status_arr.label            状态名称
     * @apiSuccess {String}  data.condition.status_arr.label            状态名称
     * @apiSuccess {String}  data.condition.department_arr              部门
     * @apiSuccess {String}  data.condition.use_member_arr              运营人
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $devicesService = new DevicesService();
        $list = $devicesService->devicesList($params, $page, $pageSize);

        // 筛选项
        $list['condition'] = [
            'project_arr' => MemberService::getProjectListCondition(),
            'status_arr' => DevicesService::getStatusCondition(),
            'use_member_arr' => DevicesService::getUseMemberCondition(),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/devices/edit-condition 编辑下拉筛选
     * @apiGroup Devices
     * @apiName edit-condition
     * @apiUse CommonParam
     *
     * @apiParam {int} devices_id                设备ID
     *
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.project_arr                 项目筛选
     * @apiSuccess {String}  data.condition.scard_arr                   手机卡筛选
     * @apiSuccess {String}  data.condition.department_arr              部门筛选
     *
     */
    public function actionEditCondition(): array
    {
        $params = Yii::$app->request->get();
        $devicesId = $params['devices_id'] ?? 0;
        $devices = Devices::findOne($devicesId);
        // 筛选项
        $list['condition'] = [
            'project_arr' => MemberService::getProjectEditCondition($devices->project_id ?? null),
            'scard_arr' => ScardService::getScardEditCondition((int)$devicesId),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/devices/get-member-condition 人员下拉列表
     * @apiGroup Devices
     * @apiName get-member-condition
     * @apiUse CommonParam
     *
     * @apiParam {int} project_id                项目ID
     * @apiParam {int} department_id             部门ID
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.value              人员ID
     * @apiSuccess {Number}  data.label              人员名称
     */
    public function actionGetMemberCondition(): array
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = isset($params['project_id']) && $params['project_id'] ? $params['project_id'] : null;
        $params['department_id'] = isset($params['department_id']) && $params['department_id'] ? $params['department_id'] : null;
        return MemberService::getMemberByProject($params['project_id'], $params['department_id']);
    }

    /**
     * @api {post} /v1/buss/devices/update-devices 更新设备
     * @apiGroup Devices
     * @apiName update-devices
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                      设备ID【编辑时必传】
     * @apiBody {String}   phone_model                             手机型号
     * @apiBody {String}   name                                    名称
     * @apiBody {String}   mobile                                  手机号
     * @apiBody {Number}   project_id                              项目ID
     * @apiBody {Number}   use_mid                                 设备使用人ID
     * @apiBody {String}   use_mname                               设备使用人
     * @apiBody {String}   remark                                  备注
     * @apiBody {Number}   status                                  状态【-1：删除、0：禁用、1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdateDevices()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '名称必填');
        }
        /*if (!isset($params['mobile']) || !$params['mobile']) {
            return CommonHelper::api(422, '手机号必填');
        }*/
        if (!isset($params['project_id']) || !$params['project_id']) {
            return CommonHelper::api(422, '项目必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $devicesService = new DevicesService();
        return $devicesService->updateDevices($params);
    }

    /**
     * @api {post} /v1/buss/devices/delete-devices 删除设备
     * @apiGroup Devices
     * @apiName delete-devices
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteDevices()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $devicesService = new DevicesService();
        return $devicesService->deleteDevices($params);
    }
}
